package com.wm.common.core.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wm.common.core.domain.entity.SysUser;
import com.wm.common.core.domain.model.LoginUser;
import com.wm.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createBy", user.getUserId());  // 动态填充 填充当前登录的用户的 ID
        metaObject.setValue("updateBy", user.getUserId());  // 动态填充 填充当前登录的用户的 ID
        metaObject.setValue("createDept", user.getDeptId());
    }


    @Override
    public void updateFill(MetaObject metaObject) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateBy", user.getUserId());
    }
}
